{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4833007b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 1 1 1 1 0 0]\n",
      " [0 1 0 0 0 0 1 0]\n",
      " [1 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 1]\n",
      " [1 0 0 0 0 0 0 1]\n",
      " [0 1 0 0 0 0 1 0]\n",
      " [0 0 1 1 1 1 0 0]]\n",
      "[[0 0 0 0 0 0 0 1]\n",
      " [0 0 0 0 0 0 1 0]\n",
      " [0 0 0 0 0 1 0 0]\n",
      " [0 0 0 0 1 0 0 0]\n",
      " [0 0 0 1 0 0 0 0]\n",
      " [0 0 1 0 0 0 0 0]\n",
      " [0 1 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]]\n",
      "[[1. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]]\n",
      "[[0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#例2-3、练习2-1\n",
    "import numpy as np\n",
    "A=np.array([[0,0,1,1,1,1,0,0],\n",
    "            [0,1,0,0,0,0,1,0],\n",
    "            [1,0,0,0,0,0,0,1],\n",
    "            [1,0,0,0,0,0,0,1],\n",
    "            [1,0,0,0,0,0,0,1],\n",
    "            [1,0,0,0,0,0,0,1],\n",
    "            [0,1,0,0,0,0,1,0],\n",
    "            [0,0,1,1,1,1,0,0]])\n",
    "B=np.array([[0,0,0,0,0,0,0,1],\n",
    "            [0,0,0,0,0,0,1,0],\n",
    "            [0,0,0,0,0,1,0,0],\n",
    "            [0,0,0,0,1,0,0,0],\n",
    "            [0,0,0,1,0,0,0,0],\n",
    "            [0,0,1,0,0,0,0,0],\n",
    "            [0,1,0,0,0,0,0,0],\n",
    "            [1,0,0,0,0,0,0,0]])\n",
    "I=np.eye(5)\n",
    "O=np.zeros((5,6))\n",
    "print(A)\n",
    "print(B)\n",
    "print(I)\n",
    "print(O)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "222e1ba5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0 0]\n",
      " [0 0 0 1 0]\n",
      " [0 0 1 0 0]\n",
      " [0 1 0 0 0]\n",
      " [0 0 0 0 1]]\n",
      "[[1 0 0 0 0]\n",
      " [0 1 0 0 0]\n",
      " [0 0 3/2 0 0]\n",
      " [0 0 0 1 0]\n",
      " [0 0 0 0 1]]\n",
      "[[1 0 0 0 0]\n",
      " [0 1 0 0 0]\n",
      " [0 0 1 0 0]\n",
      " [0 3/2 0 1 0]\n",
      " [0 0 0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "#例2-4、练习2-4\n",
    "import numpy as np\n",
    "from utility import P1,P2,P3\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "E1=np.eye(5)\n",
    "P1(E1,1,3)\n",
    "print(E1)\n",
    "E2=np.eye(5)\n",
    "P2(E2,2,3/2)\n",
    "print(E2)\n",
    "E3=np.eye(5)\n",
    "P3(E3,1,3,3/2)\n",
    "print(E3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cd534fb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1  2  1  0]\n",
      " [ 1  1  0  1]\n",
      " [ 1  0  0  0]\n",
      " [ 0  1  0  0]]\n",
      "[1 0 0 0]\n",
      "[0 1 0 0]\n",
      "[-1  2  1  0]\n",
      "[1 1 0 1]\n",
      "[[ 1]\n",
      " [ 0]\n",
      " [-1]\n",
      " [ 1]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [1]]\n",
      "[[0]\n",
      " [0]\n",
      " [1]\n",
      " [0]]\n",
      "[[0]\n",
      " [0]\n",
      " [0]\n",
      " [1]]\n"
     ]
    }
   ],
   "source": [
    "#例2-5、练习2-5\n",
    "import numpy as np\n",
    "A=np.array([[1,0,0,0],\n",
    "            [0,1,0,0],\n",
    "            [-1,2,1,0],\n",
    "            [1,1,0,1]])\n",
    "I=A[:2,:2]\n",
    "O=A[:2,2:]\n",
    "A1=A[2:,:2]\n",
    "B=np.vstack((np.hstack((A1,I)),\n",
    "             np.hstack((I,O))))\n",
    "print(B)\n",
    "a1=A[0];a2=A[1];a3=A[2];a4=A[3]\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)\n",
    "print(a4)\n",
    "b1=A[:,0];b2=A[:,1];b3=A[:,2];b4=A[:,3]\n",
    "print(b1.reshape(4,1))\n",
    "print(b2.reshape(4,1))\n",
    "print(b3.reshape(4,1))\n",
    "print(b4.reshape(4,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f972b6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7 7 7]\n",
      " [7 7 7]]\n",
      "[[-5 -3 -1]\n",
      " [ 1  3  5]]\n"
     ]
    }
   ],
   "source": [
    "#例2-10\n",
    "import numpy as np\n",
    "A=np.array([[1, 2, 3],\n",
    "            [4, 5, 6]])\n",
    "B=np.array([[6, 5, 4],\n",
    "            [3, 2, 1]])\n",
    "print(A+B)\n",
    "print(A-B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6b73e9b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1. -6. -6.]\n",
      " [-2. -5. -6.]\n",
      " [-2. -6. -5.]]\n"
     ]
    }
   ],
   "source": [
    "#练习2-8\n",
    "import numpy as np\n",
    "A=np.array([[1,3,3],\n",
    "            [1,4,3],\n",
    "            [1,3,4]])\n",
    "I=np.eye(3)\n",
    "print(3*I-2*A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fa7855d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0 0 0 0 1]\n",
      " [0 0 0 0 0 0 1 0]\n",
      " [0 0 0 0 0 1 0 0]\n",
      " [0 0 0 0 1 0 0 0]\n",
      " [0 0 0 1 0 0 0 0]\n",
      " [0 0 1 0 0 0 0 0]\n",
      " [0 1 0 0 0 0 0 0]\n",
      " [1 0 0 0 0 0 0 0]]\n",
      "[[0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]]\n",
      "[[0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "#例2-11、练习2-9\n",
    "import numpy as np\n",
    "A=np.array([[0,0,0,0,0,0,0,1],\n",
    "            [0,0,0,0,0,0,1,0],\n",
    "            [0,0,0,0,0,1,0,0],\n",
    "            [0,0,0,0,1,0,0,0],\n",
    "            [0,0,0,1,0,0,0,0],\n",
    "            [0,0,1,0,0,0,0,0],\n",
    "            [0,1,0,0,0,0,0,0],\n",
    "            [1,0,0,0,0,0,0,0]])\n",
    "print(A)\n",
    "print(A^A)\n",
    "print(0&A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "765d1b50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9 11]\n",
      " [-2 15]\n",
      " [-1 19]]\n",
      "[[ 6 -7]\n",
      " [20 -5]]\n",
      "[[  5  -2  13  12]\n",
      " [ -1   1  -3  -4]\n",
      " [ -1   4  -5 -12]\n",
      " [  8   4  16   0]]\n",
      "[[-16  16]\n",
      " [  8  -8]]\n",
      "[[  0   0]\n",
      " [ 12 -24]]\n"
     ]
    }
   ],
   "source": [
    "#例2-17\n",
    "import numpy as np\n",
    "A=np.array([[4,-1,2,1],\n",
    "            [1,1,0,3],\n",
    "            [0,3,1,4]])\n",
    "B=np.array([[1,2],\n",
    "            [0,1],\n",
    "            [3,0],\n",
    "            [-1,4]])\n",
    "print(np.matmul(A,B))\n",
    "A=np.array([[2,1,4,0],\n",
    "            [1,-1,3,4]])\n",
    "B=np.array([[1,3],\n",
    "            [0,-1],\n",
    "            [1,-3],\n",
    "            [4,0]])\n",
    "print(np.matmul(A,B))\n",
    "print(np.matmul(B,A))\n",
    "A=np.array([[-2,4],\n",
    "            [1,-2]])\n",
    "B=np.array([[2,4],\n",
    "            [-3,6]])\n",
    "print(np.matmul(A,B))\n",
    "print(np.matmul(B,A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e5c18c59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6 -7  8]\n",
      " [20 -5 -6]]\n",
      "[[ -2  13  22]\n",
      " [ -2 -17  20]\n",
      " [  4  29  -2]]\n"
     ]
    }
   ],
   "source": [
    "#练习2-14\n",
    "import numpy as np\n",
    "A=np.array([[2,1,4,0],\n",
    "            [1,-1,3,4]])\n",
    "B=np.array([[1,3,1],\n",
    "            [0,-1,2],\n",
    "            [1,-3,1],\n",
    "            [4,0,-2]])\n",
    "print(np.matmul(A,B))\n",
    "A=np.array([[1,1,1],\n",
    "            [1,1,-1],\n",
    "            [1,-1,1]])\n",
    "B=np.array([[1,2,3],\n",
    "            [-1,-2,4],\n",
    "            [0,5,1]])\n",
    "print(3*np.matmul(A,B)-2*A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ec3a81ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6 7 8]\n",
      " [1 2 3 4]\n",
      " [9 10 11 12]]\n",
      "[[2 1 3 4]\n",
      " [6 5 7 8]\n",
      " [10 9 11 12]]\n",
      "[[1 2 3 4]\n",
      " [5/2 3 7/2 4]\n",
      " [9 10 11 12]]\n",
      "[[1 1 3 4]\n",
      " [5 3 7 8]\n",
      " [9 5 11 12]]\n",
      "[[7/2 5 13/2 8]\n",
      " [5 6 7 8]\n",
      " [9 10 11 12]]\n",
      "[[1 5/2 3 4]\n",
      " [5 17/2 7 8]\n",
      " [9 29/2 11 12]]\n"
     ]
    }
   ],
   "source": [
    "#例2-18、练习2-15\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "from utility import P1,P2,P3\n",
    "A=np.array([[1,2,3,4],\n",
    "            [5,6,7,8],\n",
    "            [9,10,11,12]])\n",
    "E1=np.eye(3)\n",
    "P1(E1,0,1)\n",
    "print(np.matmul(E1,A))\n",
    "E1=np.eye(4)\n",
    "P1(E1,0,1)\n",
    "print(np.matmul(A,E1))\n",
    "E2=np.eye(3)\n",
    "P2(E2,1,1/2)\n",
    "print(np.matmul(E2,A))\n",
    "E2=np.eye(4)\n",
    "P2(E2,1,1/2)\n",
    "print(np.matmul(A,E2))\n",
    "E3=np.eye(3)\n",
    "P3(E3,1,0,1/2)\n",
    "print(np.matmul(E3,A))\n",
    "E3=np.eye(4)\n",
    "P3(E3,1,0,1/2)\n",
    "print(np.matmul(A,E3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ebb5675a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 1 3]\n",
      " [8 0 3]\n",
      " [-2 1 -2]]\n"
     ]
    }
   ],
   "source": [
    "#例2-19\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[2,1,1],\n",
    "            [3,1,2],\n",
    "            [1,-1,0]])\n",
    "I=np.eye(3)\n",
    "f=-I-A+np.matmul(A,A)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d879fe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0]\n",
      " [0 0]]\n"
     ]
    }
   ],
   "source": [
    "#练习2-16\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[2,-1],\n",
    "            [-3,3]])\n",
    "I=np.eye(2)\n",
    "f=3*I-5*A+np.matmul(A,A)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "97a99b5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0 17]\n",
      " [14 13]\n",
      " [-3 10]]\n",
      "[[ 0 17]\n",
      " [14 13]\n",
      " [-3 10]]\n"
     ]
    }
   ],
   "source": [
    "#例2-22\n",
    "import numpy as np\n",
    "A=np.array([[2,0,-1],\n",
    "            [1,3,2]])\n",
    "B=np.array([[1,7,-1],\n",
    "            [4,2,3],\n",
    "            [2,0,1]])\n",
    "print((np.matmul(A,B)).T)\n",
    "print((np.matmul(B.T, A.T)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "51fce556",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  5  8]\n",
      " [ 0 -5  6]\n",
      " [ 3 11 -4]]\n"
     ]
    }
   ],
   "source": [
    "#练习2-18\n",
    "import numpy as np\n",
    "A=np.array([[1,1,1],\n",
    "            [1,1,-2],\n",
    "            [1,-1,1]])\n",
    "B=np.array([[1,2,3],\n",
    "            [-1,-2,4],\n",
    "            [0,5,1]])\n",
    "C=np.matmul(A.T,B)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "eaa441c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40.0\n",
      "16.0\n"
     ]
    }
   ],
   "source": [
    "#例2-28、练习2-23\n",
    "import numpy as np\n",
    "A=np.array([[3,1,-1,2],\n",
    "            [-5,1,3,-4],\n",
    "            [2,0,1,-1],\n",
    "            [1,-5,3,-3]])\n",
    "print('%.1f'%np.linalg.det(A))\n",
    "A=np.array([[1,2,3,4],\n",
    "            [1,3,4,1],\n",
    "            [1,4,1,2],\n",
    "            [1,1,2,3]])\n",
    "print('%.1f'%np.linalg.det(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1e1cfde7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 6 -4]\n",
      " [-3 -6 5]\n",
      " [2 2 -2]]\n",
      "[[2 0 0]\n",
      " [0 2 0]\n",
      " [0 0 2]]\n",
      "[[2 0 0]\n",
      " [0 2 0]\n",
      " [0 0 2]]\n"
     ]
    }
   ],
   "source": [
    "#例2-36、练习2-32\n",
    "import numpy as np\n",
    "from utility import adjointMatrix\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [2,2,1],\n",
    "            [3,4,3]])\n",
    "Am=adjointMatrix(A)\n",
    "print(Am)\n",
    "print(np.matmul(A,Am))\n",
    "print(np.matmul(Am,A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "52d637a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "[[1 3 -2]\n",
      " [-3/2 -3 5/2]\n",
      " [1 1 -1]]\n"
     ]
    }
   ],
   "source": [
    "#例2-37\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [2,2,1],\n",
    "            [3,4,3]])\n",
    "print('%.1f'%np.linalg.det(A))\n",
    "print(np.linalg.inv(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fddc7525",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-2 2 1]\n",
      " [-8/3 5 -2/3]]\n"
     ]
    }
   ],
   "source": [
    "#练习2-33\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[2,1,-1],\n",
    "            [2,1,0],\n",
    "            [1,-1,1]])\n",
    "B=np.array([[1,-1,3],\n",
    "            [4,3,2]])\n",
    "A1=np.linalg.inv(A)\n",
    "print(np.matmul(B,A1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "968e5d6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5 6]\n",
      " [1 2 3]\n",
      " [7 8 9]]\n",
      "[[2 1 3]\n",
      " [5 4 6]\n",
      " [8 7 9]]\n",
      "[[1 2 3]\n",
      " [2 5/2 3]\n",
      " [7 8 9]]\n",
      "[[1 1 3]\n",
      " [4 5/2 6]\n",
      " [7 4 9]]\n",
      "[[3 9/2 6]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 5/2 3]\n",
      " [4 7 6]\n",
      " [7 23/2 9]]\n"
     ]
    }
   ],
   "source": [
    "#初等矩阵与初等变换\n",
    "import numpy as np\n",
    "from utility import P1,P2,P3\n",
    "from fractions import Fraction as F\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(F(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [4,5,6],\n",
    "            [7,8,9]])\n",
    "E1=np.eye(3)\n",
    "P1(E1,0,1)\n",
    "print(np.matmul(E1,A))\n",
    "print(np.matmul(A,E1))\n",
    "E2=np.eye(3)\n",
    "P2(E2,1,1/2)\n",
    "print(np.matmul(E2,A))\n",
    "print(np.matmul(A,E2))\n",
    "E3=np.eye(3)\n",
    "P3(E3,1,0,1/2)\n",
    "print(np.matmul(E3,A))\n",
    "print(np.matmul(A,E3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6051aad3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
